package isquirrel;

import static org.junit.Assert.assertEquals;
import isquirrel.beans.Interest;
import isquirrel.beans.User;
import isquirrel.recommender.StaticRecommender;
import isquirrel.semantic.DBpedia;
import isquirrel.util.HibernateUtil;

import java.math.BigInteger;
import java.util.Set;

import org.junit.Test;

public class LoggerTest {
	
	public User createUser1() {
		User u = new User();
		u.setFirstName("Andreas");
		u.setLastName("Matsikaris");
		
		HibernateUtil.getSession().saveOrUpdate(u);
		return u;
	}
	
	public User createUser2() {
		User u = new User();
		u.setFirstName("Andreas");
		u.setLastName("Kadis");
		
		HibernateUtil.getSession().saveOrUpdate(u);
		return u;
	}
	
	public Interest getInterest(String interest) {
		Set<String> subjects =( new DBpedia()).getSubjects(interest);

		if (subjects == null) {
			return null;
		}
		return new Interest(interest, subjects);
	}

	@Test
	public void testCountFunctions() {
		HibernateUtil.useTestDB();
		
		HibernateUtil.beginTransaction();

		User u1 = createUser1();
		User u2 = createUser2();
		
		u1.addFriend(u2);
		
		HibernateUtil.commit();
		
		HibernateUtil.beginTransaction();
		Interest interest = getInterest("Computer Science");
		StaticRecommender.insertRecommendations(u1, interest);
		HibernateUtil.commit();
		
		HibernateUtil.beginTransaction();

		assertEquals(BigInteger.valueOf(2), Logger.getUsersCount());
		assertEquals(BigInteger.valueOf(1),Logger.getFriendsCount(u1));
		assertEquals(BigInteger.valueOf(0),Logger.getInterestsCount(u1));
		
		HibernateUtil.commit();
	}
	
	@Test
	public void testLog() {
		HibernateUtil.useTestDB();
		
		HibernateUtil.beginTransaction();
		
		User u1 = createUser1();
		Logger.log(u1, "add interest");
		
		HibernateUtil.commit();
	}


}
